<?php

/**
 * uniBoard API User
 *
 * Light and fast forum solution for web 2.0-like communities.
 *
 * @package		uniBoard
 * @author		Rafał Pitoń
 * @license		http://www.gnu.org/licenses/gpl-3.0.html
 * @since		Version 0.1
 * 
 * ------------------------------------------------------------------------
 * 
 * $Date: 2010-09-20 21:23:27 +0000 (Mon, 20 Sep 2010) $
 * $Revision: 64 $
 * $Author: rafio.xudb@gmail.com $
 */

// ------------------------------------------------------------------------

class api_user extends class_api{
	
	/**
	 * Main class pointer
	 *
	 * @var object
	 */
	
	private $appCore				= NULL;
	
	/**
	 * User ID
	 *
	 * @var integer
	 */
	
	public $user_id 				= 0;
	
	/**
	 * User Name
	 *
	 * @var string
	 */
	
	public $user_name 				= '';
	
	/**
	 * User Set date
	 *
	 * @var integer
	 */
	
	public $user_name_date			= 0;
	
	/**
	 * User Login
	 *
	 * @var string
	 */
	
	public $user_login				= '';
	
	/**
	 * User Password (HASH, not rawtext)
	 *
	 * @var string
	 */
	
	public $user_password			= '';
	
	/**
	 * User Salt
	 *
	 * @var string
	 */
	
	public $user_salt				= '';
	
	/**
	 * User Password Set Date
	 *
	 * @var integer
	 */
	
	public $user_password_date		= 0;
	
	/**
	 * User Password Reset Token
	 *
	 * @var string
	 */
	
	public $user_password_token	= '';
	
	/**
	 * User E-Mail
	 *
	 * @var string
	 */
	
	public $user_email				= '';
	
	/**
	 * User Want E-Mail's
	 *
	 * @var bool
	 */
	
	public $user_want_emails		= true;
	
	/**
	 * User Show E-Mail
	 *
	 * @var bool
	 */
	
	public $user_show_email		= false;
	
	/**
	 * User Want Mailings
	 *
	 * @var bool
	 */
	
	public $user_want_mailings		= true;
	
	/**
	 * User Join Date
	 *
	 * @var integer
	 */
	
	public $user_join_date			= 0;
	
	/**
	 * User Join IP
	 *
	 * @var string
	 */
	
	public $user_join_ip			= '';
	
	/**
	 * User Last Visit Date
	 *
	 * @var integer
	 */
	
	public $user_last_date			= 0;
	
	/**
	 * User Last Visit IP
	 *
	 * @var string
	 */
	
	public $user_last_ip			= '';
	
	/**
	 * User Posts Number
	 *
	 * @var integer
	 */
	
	public $user_posts				= 0;
	
	/**
	 * Custom User Rank
	 *
	 * @var integer
	 */
	
	public $user_rank				= 0;
	
	/**
	 * Custom User Title
	 *
	 * @var integer
	 */
	
	public $user_title				= '';
	
	/**
	 * User Last Post Date
	 *
	 * @var integer
	 */
	
	public $user_last_post			= 0;
	
	/**
	 * User Last Search Date
	 *
	 * @var integer
	 */
	
	public $user_last_search		= 0;
	
	/**
	 * User Avatar Type
	 * 0 - NO AVATAR
	 * 1 - REMOTE
	 * 2 - Gallery
	 * 3 - Uploaded
	 *
	 * @var integer
	 */
	
	public $user_avatar_type		= 0;
	
	/**
	 * User Avatar File
	 *
	 * @var string
	 */
	
	public $user_avatar_image		= 0;
	
	/**
	 * User Avatar Image Width
	 *
	 * @var integer
	 */
	
	public $user_avatar_width		= 0;
		
	/**
	 * User Avatar Image Height
	 *
	 * @var integer
	 */
	
	public $user_avatar_height		= 0;
	
	/**
	 * User Raw (unparsed) Signature
	 *
	 * @var string
	 */
	
	public $user_signature_raw		= '';
	
	/**
	 * User Pre-Parsed Signature
	 *
	 * @var string
	 */
	
	public $user_signature			= '';
	
	/**
	 * User Signature Parsing Settings
	 *
	 * @var string
	 */
	
	public $user_signature_set		= '111';
	
	/**
	 * User Activation Requirement
	 * 0 - None
	 * 1 - Register - User
	 * 2 - Register - Admin
	 *
	 * @var integer
	 */
	
	public $user_activation		= 0;
	
	/**
	 * User Activation Code
	 *
	 * @var string
	 */
	
	public $user_code	= '';
	
	/**
	 * User Primary Group ID
	 *
	 * @var integer
	 */
	
	public $group_id				= 0;
	
	/**
	 * User Groups Cache
	 *
	 * @var array
	 */
	
	public $user_groups			= array();
	
	/**
	 * User Permission Masks
	 *
	 * @var array
	 */
	
	public $user_masks				= array();
	
	/**
	 * User Timezone
	 *
	 * @var float
	 */
	
	public $user_timezone			= 0;
	
	/**
	 * User DST usage
	 *
	 * @var bool
	 */
	
	public $user_dst				= false;
	
	/**
	 * User Language
	 *
	 * @var integer
	 */
	
	public $user_language			= 0;
		
	/**
	 * User Style
	 *
	 * @var integer
	 */
	
	public $user_style				= 0;
				
	// ------------------------------------------------------------------------

	/**
	 * Initialise Api
	 *
	 * @param object $appCore
	 * @param integer $target_id
	 */
	
	function __construct( $appCore, $target_id = 0){
					
		// Store appCore pointer
		$this -> appCore = $appCore;
	
		// Store User ID
		$this -> user_id = (integer) $target_id;
		
		// Autoload If User ID is bigger than zero
		if ( $target_id > 0)
		{
			// Make select
			$appCore -> db -> query( 'SELECT *
			FROM ' . DB_PREFIX . 'users
			WHERE user_id = \'' . $this -> user_id . '\' AND user_id > \'0\'
			LIMIT 1');
			
			// User found
			if ( $user_result = $appCore -> db -> fetch_assoc())
			{
				$this -> data_loaded				= true;
				
				$this -> user_id		 			= $user_result['user_id'];
				$this -> user_name		 			= $user_result['user_name'];
				$this -> user_name_date	 			= $user_result['user_name_date'];
				$this -> user_login		 			= $user_result['user_login'];
				$this -> user_password	 			= $user_result['user_password'];
				$this -> user_salt		 			= $user_result['user_salt'];
				$this -> user_password_date			= $user_result['user_password_date'];
				$this -> user_password_token		= $user_result['user_password_token'];
				$this -> user_email					= $user_result['user_email'];
				$this -> user_want_emails			= $user_result['user_want_emails'];
				$this -> user_show_email			= $user_result['user_show_email'];
				$this -> user_want_mailings			= $user_result['user_want_mailings'];
				$this -> user_join_date				= $user_result['user_join_date'];
				$this -> user_join_ip				= $user_result['user_join_ip'];
				$this -> user_last_date				= $user_result['user_last_date'];
				$this -> user_last_ip				= $user_result['user_last_ip'];
				$this -> user_posts					= $user_result['user_posts'];
				$this -> user_rank					= $user_result['user_rank'];
				$this -> user_title					= $user_result['user_title'];
				$this -> user_last_post				= $user_result['user_last_post'];
				$this -> user_last_search			= $user_result['user_last_search'];
				$this -> user_avatar_type			= $user_result['user_avatar_type'];
				$this -> user_avatar_image			= $user_result['user_avatar_image'];
				$this -> user_avatar_width			= $user_result['user_avatar_width'];
				$this -> user_avatar_height			= $user_result['user_avatar_height'];
				$this -> user_signature_raw			= $user_result['user_signature_raw'];
				$this -> user_signature				= $user_result['user_signature'];
				$this -> user_signature_set			= $user_result['user_signature_set'];
				$this -> user_activation			= $user_result['user_activation'];
				$this -> user_code					= $user_result['user_code'];
				$this -> group_id					= $user_result['group_id'];
				$this -> user_groups				= explode( ',', $user_result['user_groups']);
				$this -> user_masks					= explode( ',', $user_result['user_masks']);
				$this -> user_timezone				= $user_result['user_timezone'];
				$this -> user_dst					= $user_result['user_dst'];
				$this -> user_language				= $user_result['user_language'];
				$this -> user_style					= $user_result['user_style'];
			}
			else
			{
				//give action an error
				$this -> setError( $appCore -> lang['api_user_not_found']);
			}
			
			// Free result
			$appCore -> db -> freeResult();
		}
		
	}
	
	// ------------------------------------------------------------------------

	/**
	 * Save API data to DB
	 *
	 */
	
	function saveData(){
		
		// Do mysql query only when we have changes, and no errors
		if ( !empty( $this -> changes) && !$this -> error)
		{	
			// Insert?
			if ( $this -> user_id == 0)
			{
				// Insert new record
				$this -> appCore -> db -> insert( $this -> changes, 'users');
			
				// Store new user id
				$this -> user_id = $this -> appCore -> db -> insertId();
			}
			else
			{
				// Truncate ACL cache and force Board to build new one?
				if ( isset($this -> changes['group_id']) || isset($this -> changes['user_groups']) || isset($this -> changes['user_masks']))
				{
					$this -> changes['user_acl_cache'] = '';
				}
				
				// Update record
				$this -> appCore -> db -> update( $this -> changes, 'users', 'user_id = \'' . $this -> user_id . '\'');
			}
		}
		
	}
		
	// ------------------------------------------------------------------------

	/**
	 * Set user_name
	 *
	 * @param string $new_value
	 */
	
	function setUserName( $new_value){
		
		// Kill lines
		$new_value = class_strings::stripLines( $new_value);
		
		// Store in object?
		if ( isset( $new_value[0]))
		{
			$this -> user_name = $new_value;	
		}
		
		// Check min Lenght
		if ( mb_strlen( $new_value) >= $this -> appCore -> conf['user_name_lenght_min'])
		{
			// Check max Lenght
			if ( mb_strlen( $new_value) > $this -> appCore -> conf['user_name_lenght_max'])
			{
				// Set error
				$this -> setError( sprintf( $this -> appCore -> lang['api_user_name_too_long'], $this -> appCore -> conf['user_name_lenght_max']), 'ERROR_NAME');
			}
			else
			{
				// Check Contents?
				if ( $this -> appCore -> conf['user_name_content'] == 4 && preg_match('#^(\w|\s)+$#u', $new_value)) // Letters, Digits and Spaces
				{
					// Set error
					$this -> setError( $this -> appCore -> lang['api_user_name_only_4'], 'ERROR_NAME');
				}
				else if ( $this -> appCore -> conf['user_name_content'] == 3 && preg_match('#^((\w[^0-9])|\s)+$#u', $new_value)) // Letters and Spaces
				{
					// Set error
					$this -> setError( $this -> appCore -> lang['api_user_name_only_3'], 'ERROR_NAME');
				}
				else if ( $this -> appCore -> conf['user_name_content'] == 2 && preg_match('#^(\w)+$#u', $new_value)) // Letters and Digits
				{
					// Set error
					$this -> setError( $this -> appCore -> lang['api_user_name_only_2'], 'ERROR_NAME');
				}
				else if ( $this -> appCore -> conf['user_name_content'] == 1 && preg_match('#^(\w[^0-9])+$#u', $new_value)) // Letters
				{
					// Set error
					$this -> setError( $this -> appCore -> lang['api_user_name_only_1'], 'ERROR_NAME');
				}
				else // No need to check
				{
					// Check Name Disallowed
					if ( !IN_ADMIN && !$this -> appCore -> users -> nameValid( $new_value))
					{
						// Set error
						$this -> setError( $this -> appCore -> lang['api_user_name_disallowed'], 'ERROR_NAME');
					}
					else
					{
						// Check Name Avaiability
						if ( !$this -> appCore -> users -> nameAvaiable( $new_value, $this -> user_id))
						{
							// Set error
							$this -> setError( $this -> appCore -> lang['api_user_name_taken'], 'ERROR_NAME');
						}
						else
						{
							// Add to made changes
							$this -> changes['user_name'] = $this -> appCore -> db -> strEscape( mb_strtolower( $this -> user_name));
						}
					}
				}
			}
		}
		else
		{
			// Set error
			$this -> setError( sprintf( $this -> appCore -> lang['api_user_name_too_short'], $this -> appCore -> conf['user_name_lenght_min']), 'ERROR_NAME');
		}
		
	}
	
	// ------------------------------------------------------------------------
	
	/**
	 * Set user_name_date
	 *
	 * @param integer $new_value
	 */
	
	function setUserNameDate( $new_value){
		
		// Store in Api
		$this -> user_name_date = (integer) $new_value;	
					
		// Add to made changes
		$this -> changes['user_name_date'] = $this -> user_name_date;
		
	}
		
	// ------------------------------------------------------------------------

	/**
	 * Set user_login
	 *
	 * @param string $new_value
	 */
	
	function setUserLogin( $new_value){
		
		// Kill lines
		$new_value = class_strings::stripLines( $new_value);
		
		// Store in object?
		if ( isset( $new_value[0]))
		{
			$this -> user_login = $new_value;	
		}
		
		// Check min Lenght
		if ( mb_strlen( $new_value) >= $this -> appCore -> conf['user_login_lenght_min'])
		{
			// Check max Lenght
			if ( mb_strlen( $new_value) > $this -> appCore -> conf['user_login_lenght_max'])
			{
				// Set error
				$this -> setError( sprintf( $this -> appCore -> lang['api_user_login_too_long'], $this -> appCore -> conf['user_login_lenght_max']), 'ERROR_LOGIN');
			}
			else
			{
				// Check Contents?
				if ( $this -> appCore -> conf['user_login_content'] == 4 && preg_match('#^(\w|\s)+$#u', $new_value)) // Letters, Digits and Spaces
				{
					// Set error
					$this -> setError( $this -> appCore -> lang['api_user_login_only_4'], 'ERROR_LOGIN');
				}
				else if ( $this -> appCore -> conf['user_login_content'] == 3 && preg_match('#^((\w[^0-9])|\s)+$#u', $new_value)) // Letters and Spaces
				{
					// Set error
					$this -> setError( $this -> appCore -> lang['api_user_login_only_3'], 'ERROR_LOGIN');
				}
				else if ( $this -> appCore -> conf['user_login_content'] == 2 && preg_match('#^(\w)+$#u', $new_value)) // Letters and Digits
				{
					// Set error
					$this -> setError( $this -> appCore -> lang['api_user_login_only_2'], 'ERROR_LOGIN');
				}
				else if ( $this -> appCore -> conf['user_login_content'] == 1 && preg_match('#^(\w[^0-9])+$#u', $new_value)) // Letters
				{
					// Set error
					$this -> setError( $this -> appCore -> lang['api_user_login_only_1'], 'ERROR_LOGIN');
				}
				else // No need to check
				{
					// Check Login Avaiability
					if ( !$this -> appCore -> users -> loginAvaiable( $new_value, $this -> user_id))
					{
						// Set error
						$this -> setError( $this -> appCore -> lang['api_user_login_taken'], 'ERROR_LOGIN');
					}
					else
					{
						// Add to made changes
						$this -> changes['user_login'] = $this -> appCore -> db -> strEscape( $this -> user_login);
					}
				}
			}
		}
		else
		{
			// Set error
			$this -> setError( sprintf( $this -> appCore -> lang['api_user_login_too_short'], $this -> appCore -> conf['user_login_lenght_min']), 'ERROR_LOGIN');
		}
		
	}
	
	// ------------------------------------------------------------------------

	/**
	 * Generate user_salt and set user_password
	 *
	 * @param string $new_value
	 */
	
	function setUserPassword( $new_value){
		
		// Check min Lenght
		if ( mb_strlen( $new_value) >= $this -> appCore -> conf['user_password_lenght_min'])
		{
			// Check max Lenght
			if ( mb_strlen( $new_value) > $this -> appCore -> conf['user_password_lenght_max'])
			{
				// Set error
				$this -> setError( sprintf( $this -> appCore -> lang['api_user_password_too_long'], $this -> appCore -> conf['user_password_lenght_max']), 'ERROR_PASSWORD');
			}
			else
			{
				// Generate Salt
				$this -> user_salt = $this -> appCore -> makeSalt( 8, true);
				
				// Hash Password
				$this -> user_password = $this -> appCore -> hashPassword( $new_value, $this -> user_salt);
				
				// Set Password Date
				$this -> user_password_date = $this -> appCore -> time;
				
				// Add to made changes
				$this -> changes['user_password'] = $this -> appCore -> db -> strEscape( $this -> user_password);
				$this -> changes['user_salt'] = $this -> appCore -> db -> strEscape( $this -> user_salt);
				$this -> changes['user_password_date'] = $this -> appCore -> time;
			}
		}
		else
		{
			// Set error
			$this -> setError( sprintf( $this -> appCore -> lang['api_user_password_too_short'], $this -> appCore -> conf['user_password_lenght_min']), 'ERROR_PASSWORD');
		}
		
	}
	
	// ------------------------------------------------------------------------

	/**
	 * Generate user_password_token
	 *
	 */
	
	function setUserPasswordToken(){
		
		// Generate Token
		$this -> user_password_token = $this -> appCore -> makeSalt( 6, false);

		// Store in object
		$this -> changes['user_password_token'] = $this -> appCore -> db -> strEscape( $this -> user_password_token);
			
	}
	
	// ------------------------------------------------------------------------

	/**
	 * Clears user_password_token
	 *
	 */
	
	function clearUserPasswordToken(){
		
		// Generate Token
		$this -> user_password_token = '';

		// Store in object
		$this -> changes['user_password_token'] = '';
			
	}
			
	// ------------------------------------------------------------------------

	/**
	 * Set user_email
	 *
	 * @param string $new_value
	 */
	
	function setUserEmail( $new_value){
		
		// Kill Lines
		$new_value = class_strings::stripLines( $new_value);
		
		// Store in object?
		if ( isset( $new_value[0]))
		{
			$this -> user_email = $new_value;	
		}
		
		// Check Errors
		if ( !isset( $new_value[0]))
		{
			// Set Error	
			$this -> setError( $this -> appCore -> lang['api_user_email_empty'], 'ERROR_EMAIL');
		}
		else if ( isset( $new_value[255]))
		{
			// Set Error	
			$this -> setError( $this -> appCore -> lang['api_user_email_too_long'], 'ERROR_EMAIL');
		}
		else if ( !$this -> appCore -> conf['users_register_email_reuse'] && !$this -> appCore -> users -> emailAvaiable( $new_value, $this -> user_id))
		{
			// Set Error	
			$this -> setError( $this -> appCore -> lang['api_user_email_taken'], 'ERROR_EMAIL');
		}
		else if ( !class_strings::isMail( $new_value))
		{
			// Set Error	
			$this -> setError( $this -> appCore -> lang['api_user_email_incorrect'], 'ERROR_EMAIL');
		}
		else if ( $this -> appCore -> checkBan( $new_value, false, true))
		{
			// Set Error	
			$this -> setError( $this -> appCore -> lang['api_user_email_banned'], 'ERROR_EMAIL');
		}
		else
		{
			// Set E-Mail
			$this -> user_email = $new_value;	
						
			// Add to made changes
			$this -> changes['user_email'] = $this -> appCore -> db -> strEscape( $this -> user_email);	
		}
		
	}
	
	// ------------------------------------------------------------------------
	
	/**
	 * Set user_want_emails
	 *
	 * @param bool $new_value
	 */
	
	function setUserWantEmails( $new_value){
		
		// Store in Api
		$this -> user_want_emails = (bool) $new_value;	
					
		// Add to made changes
		$this -> changes['user_want_emails'] = $this -> user_want_emails;
		
	}
	
	// ------------------------------------------------------------------------
	
	/**
	 * Set user_show_email
	 *
	 * @param bool $new_value
	 */
	
	function setUserShowEmail( $new_value){
		
		// Store in Api
		$this -> user_show_email = (bool) $new_value;	
					
		// Add to made changes
		$this -> changes['user_show_email'] = $this -> user_show_email;
		
	}
	
	// ------------------------------------------------------------------------
	
	/**
	 * Set user_want_mailings
	 *
	 * @param bool $new_value
	 */
	
	function setUserWantMailings( $new_value){
		
		// Store in Api
		$this -> user_want_mailings = (bool) $new_value;	
					
		// Add to made changes
		$this -> changes['user_want_mailings'] = $this -> user_want_mailings;
		
	}
	
	// ------------------------------------------------------------------------
	
	/**
	 * Set user_join_date
	 *
	 * @param integer $new_value
	 */
	
	function setUserJoinDate( $new_value){
		
		// Store in Api
		$this -> user_join_date = (integer) $new_value;	
					
		// Add to made changes
		$this -> changes['user_join_date'] = $this -> user_join_date;
		
	}
	
	// ------------------------------------------------------------------------
	
	/**
	 * Set user_join_ip
	 *
	 * @param string $new_value
	 */
	
	function setUserJoinIp( $new_value){
		
		// Store in Api
		$this -> user_join_ip = $new_value;	
					
		// Add to made changes
		$this -> changes['user_join_ip'] = $this -> appCore -> db -> strEscape( $this -> user_join_ip);
		
	}
	
	// ------------------------------------------------------------------------
	
	/**
	 * Set user_last_date
	 *
	 * @param integer $new_value
	 */
	
	function setUserLastDate( $new_value){
		
		// Store in Api
		$this -> user_last_date = (integer) $new_value;	
					
		// Add to made changes
		$this -> changes['user_last_date'] = $this -> user_last_date;
		
	}
	
	// ------------------------------------------------------------------------
	
	/**
	 * Set user_last_ip
	 *
	 * @param string $new_value
	 */
	
	function setUserLastIp( $new_value){
		
		// Store in Api
		$this -> user_last_ip = $new_value;	
					
		// Add to made changes
		$this -> changes['user_last_ip'] = $this -> appCore -> db -> strEscape( $this -> user_last_ip);
		
	}
	
	// ------------------------------------------------------------------------
	
	/**
	 * Set user_posts
	 *
	 * @param integer $new_value
	 */
	
	function setUserPosts( $new_value){
		
		// Store in Api
		$this -> user_posts = (integer) $new_value;	
					
		// Add to made changes
		$this -> changes['user_posts'] = $this -> user_posts;
		
	}
		
	// ------------------------------------------------------------------------
	
	/**
	 * Set user_rank
	 *
	 * @param integer $new_value
	 */
	
	function setUserRank( $new_value){
		
		// Store in Api
		$this -> user_rank = (integer) $new_value;	
					
		// Add to made changes
		$this -> changes['user_rank'] = $this -> user_rank;
		
	}
		
	// ------------------------------------------------------------------------

	/**
	 * Set user_title
	 *
	 * @param string $new_value
	 */
	
	function setUserTitle( $new_value){
		
		// Kill Lines
		$new_value = class_strings::stripLines( $new_value);
		
		// Store in object?
		if ( isset( $new_value[0]))
		{
			$this -> user_title = $new_value;	
		}
		
		// Check Errors
		if ( mb_strlen( $new_value) > $this -> appCore -> conf['user_custom_title_lenght'])
		{
			// Set Error	
			$this -> setError( sprintf( $this -> appCore -> lang['api_user_email_too_long'], $this -> appCore -> conf['user_custom_title_lenght']), 'ERROR_TITLE');
		}
		else if ( !IN_ADMIN && !$this -> appCore -> users -> titleValid( $new_value))
		{
			// Set Error	
			$this -> setError( $this -> appCore -> lang['api_user_title_disallowed'], 'ERROR_TITLE');
		}
		else
		{
			// Store in Api
			$this -> user_title = $new_value;	
						
			// Add to made changes
			$this -> changes['user_title'] = $this -> appCore -> db -> strEscape( $this -> user_title);	
		}
		
	}
	
	// ------------------------------------------------------------------------
	
	/**
	 * Set user_last_post
	 *
	 * @param integer $new_value
	 */
	
	function setUserLastPost( $new_value){
		
		// Store in Api
		$this -> user_last_post = (integer) $new_value;	
					
		// Add to made changes
		$this -> changes['user_last_post'] = $this -> user_last_post;
		
	}
	
	// ------------------------------------------------------------------------
	
	/**
	 * Set user_last_search
	 *
	 * @param integer $new_value
	 */
	
	function setUserLastSearch( $new_value){
		
		// Store in Api
		$this -> user_last_search = (integer) $new_value;	
					
		// Add to made changes
		$this -> changes['user_last_search'] = $this -> user_last_search;
		
	}
	
	// ------------------------------------------------------------------------
	
	/**
	 * Set user_avatar_type
	 *
	 * @param integer $new_value
	 */
	
	function setUserAvatarType( $new_value){
		
		// Store in Api
		$this -> user_avatar_type = (integer) $new_value;	

		// Type Incorrect?
		if ( $this -> user_avatar_type < 0 || $this -> user_avatar_type > 3)
		{
			$this -> user_avatar_type = 0;
		}
			
		// Add to made changes
		$this -> changes['user_avatar_type'] = $this -> user_avatar_type;
		
	}
	
	// ------------------------------------------------------------------------
	
	/**
	 * Set user_avatar_image
	 *
	 * @param string $new_value
	 */
	
	function setUserAvatarImage( $new_value){
		
		// Store in Api
		$this -> user_avatar_image = $new_value;	
	
		// Add to made changes
		$this -> changes['user_avatar_image'] = $this -> appCore -> db -> strEscape( $this -> user_avatar_image);
		
	}
	
	// ------------------------------------------------------------------------
	
	/**
	 * Set user_avatar_width
	 *
	 * @param integer $new_value
	 */
	
	function setUserAvatarWidth( $new_value){
		
		// Explode dimensions
		$avatar_max = ( strpos( $this -> appCore -> conf['avatars_dimensions_size'], 'x') === false ? 100 : (integer) substr( $this -> appCore -> conf['avatars_dimensions_size'], 0, strpos( $this -> appCore -> conf['avatars_dimensions_size'], 'x')));
		
		// Limit incorrect?
		if ( $avatar_max < 1)
		{
			$avatar_max = 100;
		}
		
		// Store in Api
		$this -> user_avatar_width = (integer) $new_value;	

		// Min Width
		if ( $this -> user_avatar_width < 1)
		{
			$this -> user_avatar_width = 1;
		}
		
		// Max Width
		if ( $this -> user_avatar_width > $avatar_max)
		{
			$this -> user_avatar_width = $avatar_max;
		}
			
		// Add to made changes
		$this -> changes['user_avatar_width'] = $this -> user_avatar_width;
		
	}
	
	// ------------------------------------------------------------------------
	
	/**
	 * Set user_avatar_height
	 *
	 * @param integer $new_value
	 */
	
	function setUserAvatarHeight( $new_value){
		
		// Explode dimensions
		$avatar_max = ( strpos( $this -> appCore -> conf['avatars_dimensions_size'], 'x') === false ? 100 : (integer) substr( $this -> appCore -> conf['avatars_dimensions_size'], 1 + strpos( $this -> appCore -> conf['avatars_dimensions_size'], 'x')));
		
		// Limit incorrect?
		if ( $avatar_max < 1)
		{
			$avatar_max = 100;
		}
		
		// Store in Api
		$this -> user_avatar_height = (integer) $new_value;	

		// Min Height
		if ( $this -> user_avatar_height < 1)
		{
			$this -> user_avatar_height = 1;
		}
		
		// Max Height
		if ( $this -> user_avatar_height > $avatar_max)
		{
			$this -> user_avatar_height = $avatar_max;
		}
			
		// Add to made changes
		$this -> changes['user_avatar_height'] = $this -> user_avatar_height;
		
	}
	
	// ------------------------------------------------------------------------
	
	/**
	 * Delete Users Avatar
	 *
	 */
	
	function deleteUserAvatar(){
		
		// Do we even have avatar?
		if ( $this -> user_avatar_type > 0)
		{
			// Delete Avatar's file?
			if ( $this -> user_avatar_type == 3 && file_exists(UPLOADS_PATH . $this -> user_avatar_image) && !is_dir(UPLOADS_PATH . $this -> user_avatar_image))
			{
				class_files::unlink( UPLOADS_PATH . $this -> user_avatar_image);
			}
			
			// Store in Api
			$this -> user_avatar_type = 0;
			$this -> user_avatar_image = '';
			$this -> user_avatar_width = 0;
			$this -> user_avatar_height = 0;
			
			// Add to made changes
			$this -> changes['user_avatar_type'] = 0;
			$this -> changes['user_avatar_image'] = '';
			$this -> changes['user_avatar_width'] = 0;
			$this -> changes['user_avatar_height'] = 0;
		}
	}
	
	// ------------------------------------------------------------------------
	
	/**
	 * Set user_signature_raw
	 *
	 * @param string $new_value
	 */
	
	function setUserSignatureRaw( $new_value){
		
		// Store in Api
		$this -> user_signature_raw = $new_value;	
	
		// Too Big?
		if ( strlen( $new_value) > $this -> appCore -> conf['setting_signature_lenght_max'])
		{	
			// Set Error	
			$this -> setError( sprintf( $this -> appCore -> lang['api_user_signature_too_big'], $this -> appCore -> conf['setting_signature_lenght_max']), 'ERROR_SIGNATURE');
		}
		else
		{
			// Add to made changes
			$this -> changes['user_signature_raw'] = $this -> appCore -> db -> strEscape( $this -> user_signature_raw);
		}
		
	}
	
	// ------------------------------------------------------------------------
	
	/**
	 * Set user_signature
	 *
	 * @param string $new_value
	 */
	
	function setUserSignature( $new_value){
		
		// Store in Api
		$this -> user_signature = $new_value;	
	
		// Add to made changes
		$this -> changes['user_signature'] = $this -> appCore -> db -> strEscape( $this -> user_signature);
	
	}
	
	// ------------------------------------------------------------------------
	
	/**
	 * Set user_signature_set
	 *
	 * @param string $new_value
	 */
	
	function setUserSignatureSet( $new_value){
		
		// Store in Api
		$this -> user_signature_set = $new_value;	
	
		// Add to made changes
		$this -> changes['user_signature_set'] = $this -> appCore -> db -> strEscape( $this -> user_signature_set);
	
	}
		
	// ------------------------------------------------------------------------
	
	/**
	 * Set user_activation
	 *
	 * @param string $new_value
	 */
	
	function setUserActivation( $new_value){
		
		// Store in Api
		$this -> user_activation = (integer) $new_value;	
	
		// Fix IT?
		if ( $this -> user_activation < 0)
		{
			$this -> user_activation = 0;
		}
		
		if ( $this -> user_activation > 2)
		{
			$this -> user_activation = 0;
		}
		
		// Add to made changes
		$this -> changes['user_activation'] = $this -> user_activation;
	
	}
	
	// ------------------------------------------------------------------------
	
	/**
	 * Set user_code
	 *
	 */
	
	function setUserCode(){
		
		// Store in Api
		$this -> user_code = $this -> appCore -> makeSalt( 6, false);	
	
		// Add to made changes
		$this -> changes['user_code'] = $this -> appCore -> db -> strEscape( $this -> user_code);
	
	}
	
	// ------------------------------------------------------------------------
	
	/**
	 * Set group_id
	 *
	 * @param integer $new_value
	 */
	
	function setGroupId( $new_value){
		
		// Store in Api
		$this -> group_id = (integer) $new_value;	
	
		// Add to made changes
		$this -> changes['group_id'] = $this -> group_id;
	
	}
	
	// ------------------------------------------------------------------------
	
	/**
	 * Set user_groups
	 *
	 * @param array $new_value
	 */
	
	function setUserGroups( $new_value){
		
		// Store in Api
		$this -> user_groups = (array) $new_value;	
	
		// Add to made changes
		$this -> changes['user_groups'] = $this -> appCore -> strEscape( join( ',', $this -> user_groups));
	
	}
	
	// ------------------------------------------------------------------------
	
	/**
	 * Set user_masks
	 *
	 * @param array $new_value
	 */
	
	function setUserMasks( $new_value){
		
		// Store in Api
		$this -> user_masks = (array) $new_value;	
	
		// Add to made changes
		$this -> changes['user_masks'] = $this -> appCore -> strEscape( join( ',', $this -> user_masks));
	
	}
	
	// ------------------------------------------------------------------------
	
	/**
	 * Set user_timezone
	 *
	 * @param float $new_value
	 */
	
	function setUserTimezone( $new_value){
		
		// Store in Api
		$this -> user_timezone = (float) $new_value;	
	
		// Allowed timezones
		$timezones = $this -> appCore -> getTimeZones( false);
		
		// Timezone correct?
		if ( !isset( $timezones[$this -> user_timezone]))
		{
			$this -> user_timezone = 0;
		}
		
		// Add to made changes
		$this -> changes['user_timezone'] = $this -> user_timezone;
	
	}
	
	// ------------------------------------------------------------------------
	
	/**
	 * Set user_dst
	 *
	 * @param bool $new_value
	 */
	
	function setUserDST( $new_value){
		
		// Store in Api
		$this -> user_dst = (bool) $new_value;	
	
		// Add to made changes
		$this -> changes['user_dst'] = $this -> user_dst;
	
	}
	
	// ------------------------------------------------------------------------
	
	/**
	 * Set user_language
	 *
	 * @param integer $new_value
	 */
	
	function setUserLanguage( $new_value){
		
		// Store in Api
		$this -> user_language = (integer) $new_value;	
	
		// Add to made changes
		$this -> changes['user_language'] = $this -> user_language;
	
	}
	
	// ------------------------------------------------------------------------
	
	/**
	 * Set user_style
	 *
	 * @param integer $new_value
	 */
	
	function setUserStyle( $new_value){
		
		// Store in Api
		$this -> user_style = (integer) $new_value;	
	
		// Add to made changes
		$this -> changes['user_style'] = $this -> user_style;
	
	}
	
}